package co.yixiang.yshop.module.product.dal.mysql.productcoupon;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.module.product.controller.admin.productcoupon.vo.CanvasProductCouponPageReqVO;
import co.yixiang.yshop.module.product.controller.admin.productcoupon.vo.StoreProductCouponPageReqVO;
import co.yixiang.yshop.module.product.dal.dataobject.productcoupon.ProductCouponDO;
import co.yixiang.yshop.module.product.enums.coupon.CouponExpirationEnum;
import co.yixiang.yshop.module.product.enums.coupon.CouponGetTypeEnum;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品优惠券 Mapper
 *
 * @author moxiangrong
 */
@Mapper
public interface ProductCouponMapper extends BaseMapperX<ProductCouponDO> {


    default PageResult<ProductCouponDO> selectPage(StoreProductCouponPageReqVO reqVO) {
        LambdaQueryWrapperX<ProductCouponDO> wrapper = new LambdaQueryWrapperX<>();

        wrapper.likeIfPresent(ProductCouponDO::getCouponName, reqVO.getCouponName())
                .eqIfPresent(ProductCouponDO::getCouponType, reqVO.getCouponType())
                .eqIfPresent(ProductCouponDO::getCouponScope, reqVO.getCouponScope())
                .eqIfPresent(ProductCouponDO::getExpirationType, reqVO.getExpirationType())
                .eqIfPresent(ProductCouponDO::getReceiveType, reqVO.getReceiveType())
                .orderByDesc(ProductCouponDO::getId);

        return selectPage(reqVO, wrapper);

    }

    default PageResult<ProductCouponDO> getCouponListPage(CanvasProductCouponPageReqVO reqVO) {
        LambdaQueryWrapperX<ProductCouponDO> wrapper = new LambdaQueryWrapperX<>();
        LocalDateTime now = LocalDateTime.now();
        wrapper//只显示能在页面领取的
                .eq(ProductCouponDO::getGetType, CouponGetTypeEnum.NO_CODE)
                 .likeIfPresent(ProductCouponDO::getCouponName, reqVO.getCouponName())
                .eqIfPresent(ProductCouponDO::getCouponType, reqVO.getCouponType())
                .and(i -> i.eq(ProductCouponDO::getExpirationType, CouponExpirationEnum.BY_DAY.getValue())
                        .or(e->e.eq(ProductCouponDO::getExpirationType, CouponExpirationEnum.BY_PERMANENT.getValue()))
                        .or(e -> e.lt(ProductCouponDO::getTakingEffectTime, now)
                                .gt(ProductCouponDO::getExpirationTime, now))
                )
                .in(CollectionUtils.isNotEmpty(reqVO.getIds()), ProductCouponDO::getId, reqVO.getIds())
                .gt(ProductCouponDO::getNumber, 0)
                .orderByDesc(ProductCouponDO::getId);
        return selectPage(reqVO, wrapper);

    }

    List<ProductCouponDO> receiveList(@Param("productId") Long productId);

}
